<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if (!($section_id = $route->getAdminId()))
	goToPage("/admin/sections");

$errors = array();

if ($route->POST("action") == 'edit') {

	$name = UV::readMQ($route->POST("name"));
	$link = translitIt($name);
	$i = 1;
	while ($db->getCell("select `link` from `tip_sections` where `link` = '$link' and `id` != '$section_id' limit 1")){
		$link = translitIt($name)."-".($i++);
	}
	$text = UV::readMQ($route->POST("text"));
	$order = UV::readInt($route->POST("order"));

	if (count($errors) == 0) {

		$query = "update `tip_sections` set `name`='$name',`link`='$link',`text`='$text',`order`='$order' where `id` = '$section_id' limit 1";
		$update = $db->sendQuery($query);
		if (!$update){
			$errors[] = "Ошибка обновления записи";
		}
	}
}

$section = $db->getRow("select * from `tip_sections` where `id` = '$section_id' limit 1");
if (!$section)
	goToPage("/admin/sections");

loadPage("head.php");
?>
<h1>Редактирование раздела</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
	elseif ($route->POST("action") == 'edit')
		echo "<p class='success'>Успешно отредактировано<br /><br /></p>";
?>
<form method='POST' id="edit">
	<input type='hidden' name='action' value='edit' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'><label for='name'>Название</label></td>
			<td width='600px'>
				<input type='text' name='name' id='name' value="<?php echo $section['name']; ?>" />
			</td>
		</tr>
		<tr>
			<td><label for='text'>Текст</label></td>
			<td><textarea name='text' id='text'><?php echo $section['text']; ?></textarea></td>
		</tr>
		<tr>
			<td><label for='order'>Порядок</label></td>
			<td><input type='text' name='order' id='order' class="short" value="<?php echo $section['order']; ?>" /></td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Редактировать' /></td>
		</tr>
	</table>
</form>

<?php loadPage("footer.php"); ?>